#include "hero.h"

CHero::CHero(void)
{
	heroCoords = make_shared<CCoords>();
	cubeSize = make_shared<CCoords>();


		case MSG_INIT:
		{
			Message m("Hero","Cube",MSG_GET_SIZE);
			retMsg.push_back(m);
			heroCoords->set(0,1,0);
			m.Set("Hero","Cube",MSG_SET_CRSECTION,heroCoords);
			retMsg.push_back(m);
			m.Set("Hero","Cube",MSG_GET_CRSECTION);
			retMsg.push_back(m);
			break;
		}
}

CHero::~CHero(void)
{}

void CHero::Update()
{
}

void CHero::Draw(PArray2D& disp)
{
	int x = (disp->XSize() - cubeSize->X() )/2 + heroCoords->X();
	int y = (disp->YSize() - cubeSize->Y() )/2 + heroCoords->Y();
	(*disp)(x,y) = 'O';
}

void CHero::Rotate(int dir)
{
	int x=heroCoords->X();
	int y=heroCoords->Y();
	int z=heroCoords->Z();

	if( dir==ROT_LEFT )
	{
		int cz=cubeSize->Z();
		heroCoords->set(cz-z-1, y, x);
	}

	if( dir==ROT_RIGHT )
	{
		int cx=cubeSize->X();
		heroCoords->set(z, y, cx-x-1);
	}
	if( dir==ROT_CLKWISE)
	{
		int cx=cubeSize->X();
		heroCoords->set(y, cx-x-1, z);
	}

	if( dir==ROT_CCLKWISE)
	{
		int cy=cubeSize->Y();
		heroCoords->set(cy-y-1, x, z);
	}
}

void CHero::Move(int dir)
{
	PArray2D crSection = cube->getCrossSection();
	int x = heroCoords->X();
	int y = heroCoords->Y();

	if(dir==MOV_LEFT)
	{
		char c = (*crSection)(x-1,y);
		if(( c == ' ') || ( c == '.'))
			heroCoords->setX(x-1);
	}

	if(dir==MOV_RIGHT)
	{
		char c = (*crSection)(x+1,y);
		if(( c == ' ') || ( c == '.'))
			heroCoords->setX(x+1);
	}

	if(dir==MOV_UP)
	{
		char c = (*crSection)(x,y+1);
		if(( c == ' ') || ( c == '.'))
			heroCoords->setY(y+1);
	}

	if(dir==MOV_DOWN)
	{
		char c = (*crSection)(x,y-1);
		if(( c == ' ') || ( c == '.'))
			heroCoords->setY(y-1);
	}

}